<?php

/**
 * 影片控制器
 *
 */

namespace app\controllers\film;

use app\common\base\ApiController;
use app\common\behaviors\ApiCommonBehavior;


class FilmController extends ApiController
{

    /**
     * 获取电影信息列表;
     * 参数示例：[]、['film_type' => '动画', 'id' => [1,2,3,4,5,6,7]];
     * 以上同类之间取并集，不同类之间取交集;
     */
    public function actionList()
    {
        $this->rules = [
            ['condition','app\helpers\ArrayValidator'],
            [['page', 'size'], 'integer'],
            ['order_by', 'string'],
        ];
        $inputs = $this->validate();
        
        $info = ApiCommonBehavior::call(
            'film',
            'FilmService',
            'getByCondition',
            $inputs['condition'] ?? ['status' => 1],
            $inputs['order_by'] ?? 'id asc',
            $inputs['page'] ?? null,
            $inputs['size'] ?? null
        );

        return [
            'count' => $info['count'],
            'info' => $info['info'],
        ];
    }
    /**
     * 关键词搜索;
     * 只能通过电影名称搜索;
     */
    public function actionSearch(){
        $this->rules = [
            [['search_by', 'value'],'required'],
            [['search_by', 'value'], 'string']
        ];
        $inputs = $this->validate();

        $info = ApiCommonBehavior::call(
            'film',
            'FilmService',
            'search',
            $inputs['search_by'],
            $inputs['value']
        );

        return [
            'info' => $info
        ];
    }

    public function actionGetFilmType(){
        $info = ApiCommonBehavior::call(
            'film',
            'FilmTypeService',
            'lists',
            [
                'condition' => [],
                'orderby' => 'film_type asc'
            ]
        );

        foreach($info as &$one){
            unset($one['id']);
        }

        return [
            'info' => $info
        ];
    }

    /**
     * 通过影片id获取售票的相关信息;
     */
    public function actionTicketInfo(){
        $this->rules = [
            ['film_id','required'],
            ['film_id', 'integer']
        ];
        $inputs = $this->validate();

        $info = ApiCommonBehavior::call(
            'film',
            'FilmTicketService',
            'getByCondition',
            [
                'film_id' => $inputs['film_id'],
            ]
        );

        return [
            'info' => $info
        ];
    }
    
    /**
     * 购票接口;
     * 提供ticket id;
     */
    public function actionTicketBuyById(){
        $this->rules = [
            [['ticket_id', 'amount'], 'required'],
            [['ticket_id', 'amount'], 'integer'],
        ];
        $inputs = $this->validate();
        $userId = $this->userInfo->id;

        $info = ApiCommonBehavior::call(
            'film',
            'FilmService',
            'TicketBuyById',
            $inputs['ticket_id'],
            $inputs['amount'],
            $userId
        );

        return [
            'info' => $info
        ];
    }

    /**
     * 购票接口;
     * 提供若干信息;
     */
    public function actionTicketBuy(){
        $this->rules = [
            [['film_id', 'amount', 'show_times', 'video_hall'],'required'],
            [['film_id', 'amount', 'show_times', 'video_hall'], 'integer']
        ];
        $inputs = $this->validate();
        $userId = $this->userInfo->id;

        $info = ApiCommonBehavior::call(
            'film',
            'FilmService',
            'TicketBuy',
            [
                'film_id' => $inputs['film_id'],
                'amount' => $inputs['amount'],
                'show_times' => $inputs['show_times'],
                'video_hall' => $inputs['video_hall'],
            ],
            $userId
        );

        return [
            'info' => $info
        ];
    }

    /**
     * 返回指定的购票记录;
     * 参数是用户id或影片id;
     */
    public function actionGetTicketBuyRecord(){
        $this->rules = [
            [['film_id', 'user_id'], 'integer'],
        ];
        $inputs = $this->validate();
        $userInfo = $this->userInfo;
        if(1 == $userInfo->type && isset($inputs['user_id'])){
            return ApiCommonBehavior::error('ERROR_NO_AUTHORITY', '权限不足');
        }
        $condition = [];
        if(0 == $userInfo->type){
            isset($inputs['user_id']) ? $condition['user_id'] = $inputs['user_id'] : '';
        } else{
            $condition['user_id'] = $userInfo->id;
        }
        isset($inputs['film_id']) ? $condition['film_id'] = $inputs['film_id'] : '';
        

        $info = ApiCommonBehavior::call(
            'film',
            'RecordTicketBuyService',
            'getByCondition',
            $condition
        );
        // foreach($infos as $info){

        // }

        return [
            'info' => $info
        ];

    }

    /**
     * 
     */
    public function actionGetRecommendListToday(){
        $this->rules = [];
        $inputs = $this->validate();

        $info = ApiCommonBehavior::call(
            'film',
            'FilmService',
            'getRecommendListToday'
        );

        return [
            'info' => $info
        ];
    }


}
